package controllers

import (
	"app/internal/struct/web"
	"app/internal/web/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type RegisterController struct {
	Controller
}

func (Controller *RegisterController) GetControllerMethod() []string {

	return []string{MethodHttpPost}
}
func (Controller *RegisterController) GetControllerName() string {

	return "RegisterController"
}
func (Controller *RegisterController) GetSchema() string {

	return "/register"
}

func (Controller *RegisterController) Post(c *gin.Context) {

	obj := web.Register{}
	res := Response{ErrorCode: 0, ErrorMessage: ""}

	if service.GetServiceInstance() == nil {
		res.setErrorObject(serviceError)
		goto ServiceError
	}

	if err := c.ShouldBindJSON(&obj); err != nil {
		res.setErrorObject(jsonError)
		res.setErrorMessage(err.Error())
		goto ServiceError
	}

	if err := service.GetServiceInstance().IsVaildLicense(&obj); err != nil {
		res.setErrorObject(licenseCodeError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().CreateUUID(&obj); err != nil {
		res.setErrorObject(creatIdError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().CreateLicenseCode(&obj); err != nil {
		res.setErrorObject(licenseCodeError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().SaveLicense(&obj); err != nil {
		res.setErrorObject(savelicenseError)
		goto ServiceError
	}

	if err := service.GetServiceInstance().RegisterUser(obj); err != nil {
		res.setErrorObject(registerError)
		goto ServiceError
	}

	res.setData(obj)
	Controller.ServiceFinish(c, http.StatusOK, &res)
	return

ServiceError:
	Controller.ServiceFinish(c, http.StatusOK, &res)
	return
}
